﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using WMSUIMVC.DTO.Product;
using WMSUIMVC.DTO.ProductsType;
using XuanKeZz.Application.HTTP;

namespace WMSUIMVC.Controllers
{
    public class ProductController : Controller
    {
        protected readonly IConfiguration configuration;

        public ProductController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }
        #region 产品分类

        public IActionResult ProductTypeShows()
        {
            return View();
        }
        /// <summary>
        /// 返回产品分类数据
        /// </summary>
        /// <param name="ps">产品分类显示参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> ProductTypeShowGet(ProductsSelecet ps)
        {
            HttpClientWrapper client = new HttpClientWrapper(ps.Token);
            string url = configuration["Url"] + "cpd/api/ProductRead/GetAllProduct?pageindex=" + ps.PageIndex + "&pagesize=" + ps.PageSize + "&productTypeName=" + ps.ProductTypeName + "&ProductTypeCode=" + ps.ProductTypeCode;
            var response = await client.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 产品分类删除
        /// </summary>
        /// <param name="pd">产品分类删除参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> ProductTypeDelete(ProductDelete pd)
        {
            HttpClientWrapper client = new HttpClientWrapper(pd.Token);
            string url = configuration["Url"] + "cpx/api/Product/ProductDelete";
            //将产品分类id转为集合
            int[] data = { pd.ProductTypeID };
            var list = "{\"productTypeID\":" + JsonConvert.SerializeObject(data) + "}";
            //将集合转为json
            var response = await client.PostAsync(url, list);
            return Json(response);
        }
        /// <summary>
        /// 产品分类批量删除
        /// </summary>
        /// <param name="pdb">产品批量删除参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> ProductTypeDeletes(ProductBathDelete pdb)
        {
            HttpClientWrapper client = new HttpClientWrapper(pdb.Token);
            string url = configuration["Url"] + "cpx/api/Product/ProductDelete";
            var list = "{\"productTypeID\":[" + pdb.ProductTypeID + "]}";
            //将集合转为json
            var response = await client.PostAsync(url, list);
            return Json(response);
        }
        /// <summary>
        /// 产品分类添加
        /// </summary>
        /// <param name="ptc">产品分类添加参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> ProductTypeAdd(ProductTypeCreate ptc)
        {
            HttpClientWrapper client = new HttpClientWrapper(ptc.Token);
            string url = configuration["Url"] + "cpx/api/Product/ProductTypeAdd";
            var response = await client.PostAsync(url, ptc.Products);
            return Json(response);
        }

        /// <summary>
        /// 产品分类下拉
        /// </summary>
        /// <param name="model">token值</param>
        /// <returns></returns>

        [HttpGet]
        public async Task<ActionResult> ProductTypeSelect(string model)
        {
            HttpClientWrapper client = new HttpClientWrapper(model);
            string url = configuration["Url"] + "cpd/api/ProductRead/GetProductTypeSelect";
            var response = await client.GetAsync(url);
            return Json(response);
        }

        /// <summary>
        /// 产品分类反填
        /// </summary>
        /// <param name="pts">产品反填参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> ProductTypeSet(ProductTypeSet pts)
        {
            HttpClientWrapper client = new HttpClientWrapper(pts.Token);
            string url = configuration["Url"] + "cpd/api/ProductRead/SetAllProduct?id=" + pts.Id;
            var response = await client.GetAsync(url);
            return Json(response);
        }

        /// <summary>
        /// 产品分类修改
        /// </summary>
        /// <param name="ptd">产品分类修改参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> ProductTypeUpd(ProductTypeUpdate ptd)
        {
            HttpClientWrapper client = new HttpClientWrapper(ptd.Token);
            string url = configuration["Url"] + "cpx/api/Product/ProductUpdate";
            var response = await client.PostAsync(url, ptd.Products);
            return Json(response);
        }
        #endregion

        #region 产品
        /// <summary>
        /// 产品视图
        /// </summary>
        /// <returns></returns>
        public IActionResult ProductShow()
        {
            return View();
        }
        /// <summary>
        /// 返回产品数据
        /// </summary>
        /// <param name="pis">产品显示参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> ProductShowGet(ProductInfoSelect pis)
        {
            HttpClientWrapper client = new HttpClientWrapper(pis.Token);
            string url = configuration["Url"] + "cpd/api/ProductRead/GetAllProductInfo?pageindex=" + pis.PageIndex + "&pagesize=" + pis.PageSize + "&ProductsName=" + pis.ProductsName + "&ProductsCode=" + pis.ProductsCode;
            var response = await client.GetAsync(url);
            return Json(response);
        }

        /// <summary>
        /// 产品删除
        /// </summary>
        /// <param name="pid">产品删除参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> ProductsDelete(ProductInfoDelete pid)
        {
            HttpClientWrapper client = new HttpClientWrapper(pid.Token);
            string url = configuration["Url"] + "cpx/api/ProductInfo/ProductInfoDelete";
            //将产品分类id转为集合
            int[] data = { pid.ProductsID };
            var list = "{\"productsID\":" + JsonConvert.SerializeObject(data) + "}";
            //将集合转为json
            var response = await client.PostAsync(url, list);
            return Json(response);
        }
        /// <summary>
        /// 产品分类批量删除
        /// </summary>
        /// <param name="pid">产品批量删除参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> ProductDeletes(ProductInfoBathDelete pid)
        {
            HttpClientWrapper client = new HttpClientWrapper(pid.Token);
            string url = configuration["Url"] + "cpx/api/ProductInfo/ProductInfoDelete";
            var list = "{\"productsID\":[" + pid.ProductsID + "]}";
            //将集合转为json
            var response = await client.PostAsync(url, list);
            return Json(response);
        }
        /// <summary>
        /// 仓库列表下拉绑定
        /// </summary>
        /// <param name="pds">仓库下拉绑定参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetDatalist(ProductDateSelect pds)
        {

            HttpClientWrapper client = new HttpClientWrapper(pds.Token);
            string url = configuration["Url"] + "ckd/api/ReadWarehouse/GetWarehouseList?1&pageIndex=" + pds.PageIndex + "&pageSize=" + pds.PageSize;
            string Getwarehouse = await client.GetAsync(url);
            return Json(Getwarehouse);
        }
        /// <summary>
        /// 库位信息列表下拉绑定
        /// </summary>
        /// <param name="pshs">库位信息列表参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetStorehouse(ProductStoreHouseSelect pshs)
        {

            HttpClientWrapper client = new HttpClientWrapper(pshs.Token);
            string url = configuration["Url"] + "ckd/api/ReadStorehouse/GetStorehouseList?pageIndex=" + pshs.PageIndex + "&PageSize=" + pshs.PageSize;
            string Getwarehouse = await client.GetAsync(url);
            return Json(Getwarehouse);
        }
        /// <summary>
        /// 供应商信息下拉绑定
        /// </summary>
        /// <param name="pvs">供应商信息参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetVendorShow(ProductVendorSelect pvs)
        {
            HttpClientWrapper client = new HttpClientWrapper(pvs.Token);
            string url = configuration["Url"] + "ywd/api/Vendor/GetShowVendor?PageIndex=" + pvs.PageIndex + "&PageSize=" + pvs.PageSize;
            string GetVendor = await client.GetAsync(url);
            return Json(GetVendor);
        }
        /// <summary>
        /// 客户信息下拉绑定            
        /// </summary>
        /// <param name="pcs">客户信息参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetClientShow(ProductClientSelect pcs)
        {
            HttpClientWrapper client = new HttpClientWrapper(pcs.Token);
            string url = configuration["Url"] + "ywd/api/ClientRead/GetClient?pageindex=" + pcs.PageIndex + "&pagesize=" + pcs.PageSize;
            string GetClient = await client.GetAsync(url);
            return Json(GetClient);
        }
        /// <summary>
        /// 产品添加
        /// </summary>
        /// <param name="pa">产品添加参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> ProductAdd(ProductInfoAdd pa)
        {
            HttpClientWrapper client = new HttpClientWrapper(pa.Token);
            string url = configuration["Url"] + "cpx/api/ProductInfo/ProductAdd";
            var response = await client.PostAsync(url, pa.Products);
            return Json(response);
        }

        /// <summary>
        /// 产品反填
        /// </summary>
        /// <param name="ps">产品反填参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetClientSet(ProductInfoSet ps)
        {
            HttpClientWrapper client = new HttpClientWrapper(ps.Token);
            string url = configuration["Url"] + "cpd/api/ProductRead/SetAllProducts?id=" + ps.Id;
            string GetClient = await client.GetAsync(url);
            return Json(GetClient);
        }

        /// <summary>
        /// 产品修改
        /// </summary>
        /// <param name="piu">产品修改参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> ProductUpd(ProductInfoUpdate piu)
        {
            HttpClientWrapper client = new HttpClientWrapper(piu.Token);
            string url = configuration["Url"] + "cpx/api/ProductInfo/ProductInfoUpdate";
            var response = await client.PostAsync(url, piu.Products);
            return Json(response);
        }
        #endregion
    }
}
